package com.bwie.controller;

import com.bwie.config.OSSFileUtil;
import com.bwie.pojo.Brand;
import com.bwie.utils.R;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import java.io.IOException;

/**
 * @program: day0423-pxx
 * @ClassName UploadController
 * @description:
 * @author: 段阳
 * @create: 2024-04-25 20:58
 **/
@RestController
@RequestMapping("/upload")
public class UploadController {


    @Autowired
    private OSSFileUtil ossFileUtil;

    @PostMapping("/image")
    public ResponseEntity<String> upload(@RequestParam("file")MultipartFile file) throws IOException {
        String imgPath = ossFileUtil.uploadFile(file);
        return ResponseEntity.ok(imgPath);
    }

    @PostMapping("/delPhoto")
    public R delPhoto(@RequestBody Brand brand) throws IOException {
        if (StringUtils.isBlank(brand.getImage())){
            return R.error(500,"error");
        }
        String s = ossFileUtil.deleteFile(brand.getImage());
        if (s.equals("success")){
            return R.success("删除成功");
        }
            return R.error(500,"删除图片失败");

    }
}